/*var mongoose=require('mongoose');
 var config=require('../config');
 var mongodb = require('mongodb');*/
var mongoose = require('./mongoConn');
var mongoosePaginate = require('mongoose-paginate');
var Schema = mongoose.Schema;


var userSchema = new Schema({
    user: String, //管理员账号
    password: String, //管理员密码
    name: String, //购买人
    phone: String, //联系电话
    company: String, //所属公司
    roles: [{type: Schema.Types.ObjectId, ref: 'UserRole'}],//角色
    create_date: Date//创建日期
});
userSchema.plugin(mongoosePaginate);

var User = mongoose.model("User", userSchema);

User.prototype.findUserByName = function (username, callback) {
    User.findOne({user: username}, function (err, datas) {
        if (datas) {
            return callback(err, datas);
        }
        return callback(err);
    });
}
User.prototype.findAllUser = function (callback) {
    User.find({}, function (err, datas) {
        return callback(err, datas);
    })
}

User.prototype.updatePwd = function (admin, password, callback) {
    User.update({user: admin}, {password: password}, function (err) {
        return callback(err);
    })
}


User.prototype.findUserById = function (buyer, callback) {
    User.findOne({_id: buyer}, function (err, datas) {
        return callback(datas);
    });
}

User.prototype.findUserBy_Id = function (id, callback) {
    User.findOne({_id: id}, function (err, datas) {
        return callback(datas);
    });
}

User.prototype.removeById = function (buyerid, callback) {
    User.remove({buyer_id: buyerid}, function (err) {
        return callback(err);
    });
}

User.prototype.findById = function (buyer_id, callback) {
    User.findOne({buyer_id: buyer_id}, function (err, datas) {
        return callback(datas);
    });
}

User.prototype.findPwdByAcc = function (account, callback) {
    User.findOne({account: account}, function (err, datas) {
        return callback(err, datas);
    });
}

User.prototype.findAll = function (callback) {
    User.find(function (err, datas) {
        return callback(datas);
    })
}

User.prototype.findUserByUser = function (buyer, callback) {
    User.findOne({buyer: buyer}, function (err, doc) {
        if (doc) {
            return callback(err, doc);
        }
        return callback(err);
    });
}

User.prototype.findAccountByAccount = function (username, callback) {
    User.findOne({account: username}, function (err, doc) {
        if (doc) {
            return callback(err, doc);
        }
        return callback(err);
    });
}

User.prototype.updateUserById = function (id, buyer, phoneNumber, company, level, callback) {
    User.update({_id: id}, {buyer: buyer, phoneNumber: phoneNumber, company: company, level: level}, function (err) {
        return callback(err);
    })
}


User.prototype.findForPage = function (obj, callback) {
    var q = obj.search || {};                                       //查询条件
    var col = obj.columns || {};                                    //返回字段
    var query = User.find(q, col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function (err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            User.count(q, function (err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null, count, pageCount, datas);
                }
            });
        }
    });
}


module.exports = User;